package com.ukao.steward.ui.valetRunners.gathering;


import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ukao.steward.R;
import com.ukao.steward.base.BaseFragment;
import com.ukao.steward.bean.OrderInfoBean;
import com.ukao.steward.consts.Constant;
import com.ukao.steward.eventbus.MainEvent;
import com.ukao.steward.eventbus.PayEvent;
import com.ukao.steward.ui.main.HomeFragment;
import com.ukao.steward.ui.valetRunners.createOrder.PayOrderFragment;
import com.ukao.steward.ui.valetRunners.storage.StorageListFragment;
import com.ukao.steward.ui.valetRunners.waitingOrder.OrderdetailFragment;
import com.ukao.steward.util.CheckUtils;
import com.ukao.steward.util.L;
import com.ukao.steward.widget.ZProgressHUD;

import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;
import butterknife.Unbinder;
import me.yokeyword.fragmentation.SupportFragment;

import static com.ukao.steward.R.id.pay_amount;

/**
 * 收款
 */
public class gatheringFragment extends BaseFragment implements DialogInterface.OnDismissListener {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.back_btn)
    ImageView backBtn;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.title_layout)
    RelativeLayout titleLayout;
    @BindView(pay_amount)
    TextView payAmount;
    Unbinder unbinder;
    @BindView(R.id.isEnableAli)
    RadioButton isEnableAli;
    @BindView(R.id.isEnableWx)
    RadioButton isEnableWx;
    @BindView(R.id.isEnableCash)
    RadioButton isEnableCash;
    @BindView(R.id.isEnableUnion)
    RadioButton isEnableUnion;
    @BindView(R.id.layout)
    FrameLayout layout;
    private String mParam1;
    private String mParam2;
    private OrderInfoBean mOrderInfoBean;
    private SupportFragment[] mFragments = new SupportFragment[4];
    private Handler mHandler = new Handler();
    private ZProgressHUD progressDialog;
    private boolean[] payShowArray;
    private int firstPosition;
    private RadioButton[] payTabArray;


    public static gatheringFragment newInstance(OrderInfoBean mOrderInfoBean, String param2) {
        gatheringFragment fragment = new gatheringFragment();
        Bundle args = new Bundle();
        args.putParcelable(Constant.ARG_PARAM1, mOrderInfoBean);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mOrderInfoBean = getArguments().getParcelable(Constant.ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_gatherings, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    @Override
    protected void initView() {
        payAmount.setText(CheckUtils.isEmptyNumber(mOrderInfoBean.getShouldPrice()));
        titleLayout.setBackgroundColor(getColors(R.color.colorPrimary));
        title.setText("收款");
        payShowArray = new boolean[]{mOrderInfoBean.isIsEnableAli(), mOrderInfoBean.isIsEnableWx(), mOrderInfoBean.isIsEnableCash(), mOrderInfoBean.isIsEnableUnion()};
        payTabArray = new RadioButton[]{isEnableAli, isEnableWx, isEnableCash, isEnableUnion};
        boolean first = false;
        for (int i = 0; i < payShowArray.length; i++) {
            if (payShowArray[i]) {
                payTabArray[i].setVisibility(View.VISIBLE);

            }
            if (!first && payShowArray[i]) {
                first = true;
                firstPosition = i;
                payTabArray[i].setChecked(true);
            }
        }


        SupportFragment firstFragment = findFragment(AlipayWeChatFragment.class);
        if (firstFragment == null) {
            mFragments[0] = AlipayWeChatFragment.newInstance(mOrderInfoBean, true);
            mFragments[1] = AlipayWeChatFragment.newInstance(mOrderInfoBean, false);
            mFragments[2] = CashFragment.newInstance(mOrderInfoBean, "");
            mFragments[3] = UnionpayFragment.newInstance(mOrderInfoBean, "");
            loadMultipleRootFragment(R.id.layout, firstPosition,
                    mFragments[0],
                    mFragments[1],
                    mFragments[2],
                    mFragments[3]);
        } else {
            mFragments[0] = firstFragment;
            mFragments[1] = findFragment(AlipayWeChatFragment.class);
            mFragments[2] = findFragment(CashFragment.class);
            mFragments[3] = findFragment(UnionpayFragment.class);

        }
    }


    @Override
    protected boolean useEventBus() {
        return true;
    }

    @Subscribe
    public void onEvent(PayEvent event) {
        switch (event.getMessage()) {
            case PAY_SUCCEED:
                mOrderInfoBean.setPayFinish(true);
                if (mOrderInfoBean.isPayFinisEnterOrderDetails()) {
                        if (findFragment(OrderdetailFragment.class)==null){
                            popTo(StorageListFragment.class, true, () ->
                                    start( OrderdetailFragment.createBuilder().orderId(mOrderInfoBean.getOrderId()).isPopToHome(true).build()));
                        }else {
                            popTo(OrderdetailFragment.class, true, () ->
                                    start( OrderdetailFragment.createBuilder().orderId(mOrderInfoBean.getOrderId()).isPopToHome(false).build()));
                        }


                }else{
                    progressDialog = ZProgressHUD.getInstance(_mActivity);
                    progressDialog.setOnDismissListener(this);
                    progressDialog.showWithSuccess("支付成功");
                    mHandler.postDelayed(() -> {
                        progressDialog.dismiss();
                        finish();
                    }, 1000);
                }


                break;

        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mOrderInfoBean != null && mOrderInfoBean.isbundle()&&!mOrderInfoBean.isPayFinish()){
            MainEvent.postHasData(MainEvent.Message.UN_BUNDLE, mOrderInfoBean.getOrderId());
        }
        mHandler.removeCallbacksAndMessages(null);
        unbinder.unbind();
    }


    @OnClick({R.id.back_btn, R.id.title})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                finish();
                break;

        }
    }

    @OnCheckedChanged({R.id.isEnableAli, R.id.isEnableWx, R.id.isEnableCash, R.id.isEnableUnion})
    public void OnCheckedChanged(CompoundButton view, boolean checked) {
        if (checked) {
            switch (view.getId()) {
                case R.id.isEnableAli:
                    showHideFragment(mFragments[0]);
                    break;
                case R.id.isEnableWx:
                    showHideFragment(mFragments[1]);
                    break;
                case R.id.isEnableCash:
                    showHideFragment(mFragments[2]);
                    break;
                case R.id.isEnableUnion:
                    showHideFragment(mFragments[3]);
                    break;
            }
        }
    }


    @Override
    public void onDismiss(DialogInterface dialog) {

        PayEvent.postHasData(PayEvent.Message.PAY_FINISG, mOrderInfoBean);
    }


}
